package com.tj.shopping.web.controller;

import com.tj.shopping.entity.Order;
import com.tj.shopping.service.OrderService;
import com.tj.shopping.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @program: IntelliJ IDEA
 * @description:
 * @author: H
 * @create: 2023-12-26 21:25
 */
@WebServlet("/order")
public class OrderController extends BaseController{
    private OrderService service=new OrderServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String op=req.getParameter("op");
        if("finds".equals(op)){
            doFinds(req,resp);
        }else if("remove".equals(op)){
            doRemove(req,resp);
        }else if("findreturn".equals(op)){
            doFindReturn(req,resp);
        }else if ("findall".equals(op)) {
            doFindall(req, resp);
        }else if ("add".equals(op)) {
            doAdd(req, resp);
        } else if ("update".equals(op)) {
            doUpdate(req,resp);
        }else if ("find".equals(op)) {
            doFind(req,resp);
        }else {
            resp.getWriter().print("no such method");
        }

    }
    private void doFind(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String,Object> m = service.find();
        printToJson(resp, m);
    }


    private void doUpdate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Order order = parseRequest(req,Order.class);
        String ostate = req.getParameter("ostate");
        order.setOstate(Integer.parseInt(ostate));
        int result = service.update(order);
        // 响应结果给页面
        printToJson(resp, result);
    }
    //添加订单
    protected void doAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Order order = parseRequest(req,Order.class);
        System.out.println(order);
        int result =service.add(order);//
        printToJson(resp,result);
    }

    private void doFindall(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String,Object> m = service.findall();

        System.out.println(m);
        printToJson(resp,m);//响应给页面的是m
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
    protected void doFinds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Map<String ,Object> m=parseRequest(req);

        Map<String,Object>map =service.finds(m);
        System.out.println(map);

        printToJson(resp,map);
    }

    protected void doRemove(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Map<String, Object> map = parseRequest(req);
        int result=service.remove(map);
            printToJson(resp,result);

    }

    protected void doFindReturn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Map<String ,Object> m=parseRequest(req);

        Map<String,Object>map =service.findReturn(m);

        printToJson(resp,map);
    }









}
